package edu.xust.service;

import edu.xust.pojo.DataBean;
import edu.xust.utils.DateUtils;
import edu.xust.utils.HbaseUtils;
import edu.xust.utils.JedisUtils;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class HbaseService {
    public List getVipData() throws IOException, ParseException {
        List<DataBean>list = new ArrayList();
        Connection conn = HbaseUtils.getConn();
        Table vipTable = conn.getTable(TableName.valueOf("vip"));
        Scan scan = new Scan();
        //只显示当天的课程点击信息
        Date date = new Date();
        String strDate = DateUtils.converDateFormat("yyyyMM", date);
        Filter filter = new PrefixFilter(Bytes.toBytes(strDate));
        scan.setFilter(filter);
        ResultScanner resultScanner = vipTable.getScanner(scan);
        for (Result result : resultScanner) {
            Jedis jedis = JedisUtils.getJedis();
            byte[] family = Bytes.toBytes("f1");
            byte[] qualifier = Bytes.toBytes("count");
            byte[] value = result.getValue(family, qualifier);
            long count = Bytes.toLong(value);
            String row = Bytes.toString(result.getRow());
            String courseId = row.substring(8);
            String courseName =  jedis.get(courseId);
            list.add(new DataBean(courseName,count));
            jedis.close();
        }
        return list;
    }

    public List getFreeData() throws IOException, ParseException {
        List<DataBean>list = new ArrayList();
        Connection conn = HbaseUtils.getConn();
        Table vipTable = conn.getTable(TableName.valueOf("free"));
        Scan scan = new Scan();
        //只显示当天的课程点击信息
        Date date = new Date();
        String strDate = DateUtils.converDateFormat("yyyyMM", date);
        Filter filter = new PrefixFilter(Bytes.toBytes(strDate));
        scan.setFilter(filter);

        ResultScanner resultScanner = vipTable.getScanner(scan);
        for (Result result : resultScanner) {
            Jedis jedis = JedisUtils.getJedis();
            byte[] family = Bytes.toBytes("f1");
            byte[] qualifier = Bytes.toBytes("count");
            byte[] value = result.getValue(family, qualifier);
            long count = Bytes.toLong(value);
            String row = Bytes.toString(result.getRow());
            String courseId = row.substring(8);
            String courseName = jedis.get(courseId);
            list.add(new DataBean(courseName,count));
            jedis.close();
        }
        return list;
    }

    public List getCompanyData() throws IOException, ParseException {
        List list = new ArrayList();
        Connection conn = HbaseUtils.getConn();
        Table vipTable = conn.getTable(TableName.valueOf("company"));
        Scan scan = new Scan();
        //只显示当天的课程点击信息
        Date date = new Date();
        String strDate = DateUtils.converDateFormat("yyyyMM", date);
        Filter filter = new PrefixFilter(Bytes.toBytes(strDate));
        scan.setFilter(filter);
        ResultScanner resultScanner = vipTable.getScanner(scan);
        for (Result result : resultScanner) {
            Jedis jedis = JedisUtils.getJedis();
            byte[] family = Bytes.toBytes("f1");
            byte[] qualifier = Bytes.toBytes("count");
            byte[] value = result.getValue(family, qualifier);
            long count = Bytes.toLong(value);
            String row = Bytes.toString(result.getRow());
            String courseId = row.substring(8);
            String courseName = jedis.get(courseId);
            list.add(new DataBean(courseName,count));
            jedis.close();
        }
        return list;
    }
}
